package com.rwcw.admin.config.engine;

import com.rw.engine.ip.interceptor.IpInterceptor;
import com.rw.engine.ip.resolver.IpArgumentResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * IP设置类
 */
@Configuration
public class IpConfig implements WebMvcConfigurer {
    /**
     * 注入IP拦截器
     */
    @Bean
    public IpInterceptor getIpInterceptor() {
        return new IpInterceptor();
    }

    /**
     * 注入IP参数解析器
     */
    @Bean
    public IpArgumentResolver getIpArgumentResolver() {
        return new IpArgumentResolver();
    }

    /**
     * 添加拦截器
     * 让 @IpLimit 注解生效
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(getIpInterceptor()).addPathPatterns("/**");
    }

    /**
     * 添加参数解析器
     * 让 @IpParam 注解生效
     */
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(getIpArgumentResolver());
    }
}
